home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH8 / SRC / BINTREE2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-03-05  |  5.0 KB  |  168 lines

  1. VERSION 4.00
  2. Begin VB.Form BinTreeForm 
  3.    Caption         =   "Binary Tree"
  4.    ClientHeight    =   4335
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1275
  7.    ClientWidth     =   7470
  8.    Height          =   5025
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   289
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   498
  14.    Top             =   645
  15.    Width           =   7590
  16.    Begin VB.CheckBox TaperCheck 
  17.       Caption         =   "Taper Branches"
  18.       Height          =   255
  19.       Left            =   240
  20.       TabIndex        =   3
  21.       Top             =   1200
  22.       Width           =   1455
  23.    End
  24.    Begin VB.TextBox DThetaText 
  25.       Height          =   285
  26.       Left            =   1320
  27.       MaxLength       =   3
  28.       TabIndex        =   2
  29.       Text            =   "36"
  30.       Top             =   720
  31.       Width           =   615
  32.    End
  33.    Begin VB.TextBox ScaleText 
  34.       Height          =   285
  35.       Left            =   1320
  36.       MaxLength       =   5
  37.       TabIndex        =   1
  38.       Text            =   "0.75"
  39.       Top             =   360
  40.       Width           =   615
  41.    End
  42.    Begin VB.PictureBox Canvas 
  43.       AutoRedraw      =   -1  'True
  44.       Height          =   4335
  45.       Left            =   2040
  46.       ScaleHeight     =   285
  47.       ScaleMode       =   3  'Pixel
  48.       ScaleWidth      =   357
  49.       TabIndex        =   6
  50.       Top             =   0
  51.       Width           =   5415
  52.    End
  53.    Begin VB.CommandButton CmdGo 
  54.       Caption         =   "Go"
  55.       Default         =   -1  'True
  56.       Height          =   495
  57.       Left            =   600
  58.       TabIndex        =   4
  59.       Top             =   1680
  60.       Width           =   735
  61.    End
  62.    Begin VB.TextBox LevelText 
  63.       Height          =   285
  64.       Left            =   1320
  65.       MaxLength       =   3
  66.       TabIndex        =   0
  67.       Text            =   "5"
  68.       Top             =   0
  69.       Width           =   615
  70.    End
  71.    Begin VB.Label Label3 
  72.       Caption         =   "DTHETA"
  73.       Height          =   255
  74.       Left            =   0
  75.       TabIndex        =   8
  76.       Top             =   720
  77.       Width           =   735
  78.    End
  79.    Begin VB.Label Label2 
  80.       Caption         =   "LENGTH_SCALE"
  81.       Height          =   255
  82.       Left            =   0
  83.       TabIndex        =   7
  84.       Top             =   360
  85.       Width           =   1335
  86.    End
  87.    Begin VB.Label Label1 
  88.       Caption         =   "Level"
  89.       Height          =   255
  90.       Left            =   0
  91.       TabIndex        =   5
  92.       Top             =   0
  93.       Width           =   495
  94.    End
  95.    Begin VB.Menu mnuFile 
  96.       Caption         =   "&File"
  97.       Begin VB.Menu mnuFileExit 
  98.          Caption         =   "E&xit"
  99.       End
  100.    End
  101. Attribute VB_Name = "BinTreeForm"
  102. Attribute VB_Creatable = False
  103. Attribute VB_Exposed = False
  104. Option Explicit
  105. Const PI = 3.14159
  106. Const PI_2 = PI / 2
  107. Const PI_5 = PI / 5
  108. Dim LengthScale As Single
  109. Dim DTheta As Single
  110. Dim TheLevel As Integer
  111. Dim StartX As Integer
  112. Dim StartY As Integer
  113. Dim StartLength As Integer
  114. ' ************************************************
  115. ' Recursively draw a binary tree branch.
  116. ' ************************************************
  117. Sub DrawBranch(thickness As Integer, level As Integer, x As Integer, y As Integer, length As Integer, theta As Single)
  118. Dim x1 As Integer
  119. Dim y1 As Integer
  120. Dim status As Integer
  121.     ' See where this branch should end.
  122.     x1 = x + length * Cos(theta)
  123.     y1 = y + length * Sin(theta)
  124.     If thickness > 0 Then Canvas.DrawWidth = thickness
  125.     Canvas.Line (x, y)-(x1, y1)
  126.     ' If level > 1, draw the attached branches.
  127.     If level > 1 Then
  128.         DrawBranch thickness - 1, level - 1, x1, y1, length * LengthScale, theta + DTheta
  129.         DrawBranch thickness - 1, level - 1, x1, y1, length * LengthScale, theta - DTheta
  130.     End If
  131. End Sub
  132. Private Sub CmdGo_Click()
  133. Dim taper As Integer
  134.     Canvas.Cls
  135.     MousePointer = vbHourglass
  136.     DoEvents
  137.     If Not IsNumeric(LevelText.Text) Then _
  138.         LevelText.Text = "5"
  139.     TheLevel = CInt(LevelText.Text)
  140.     If Not IsNumeric(ScaleText.Text) Then _
  141.         ScaleText.Text = "0.75"
  142.     LengthScale = CSng(ScaleText.Text)
  143.     If Not IsNumeric(DThetaText.Text) Then _
  144.         DThetaText.Text = "36"
  145.     DTheta = CSng(DThetaText.Text) * PI / 180#
  146.     If TaperCheck.Value = vbChecked Then
  147.         taper = TheLevel
  148.     Else
  149.         taper = 0
  150.     End If
  151.     StartLength = (Canvas.ScaleHeight - 10) / _
  152.         ((1 - LengthScale ^ (TheLevel + 1)) / (1 - LengthScale))
  153.     DrawBranch taper, TheLevel, StartX, StartY, StartLength, -PI_2
  154.     MousePointer = vbDefault
  155. End Sub
  156. Private Sub Form_Load()
  157.     TheLevel = CInt(LevelText.Text)
  158. End Sub
  159. Private Sub Form_Resize()
  160.     Canvas.Move Canvas.Left, 0, _
  161.         ScaleWidth - Canvas.Left, ScaleHeight
  162.     StartX = Canvas.ScaleWidth \ 2
  163.     StartY = Canvas.ScaleHeight - 5
  164. End Sub
  165. Private Sub mnuFileExit_Click()
  166.     Unload Me
  167. End Sub
  168.